package cn.itsource.controller;

import cn.itsource.domain.BlogParam;
import cn.itsource.query.BlogParamQuery;
import cn.itsource.service.BlogParamService;
import cn.itsource.util.AjaxResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 控制层
 */
@RestController //所有的方法都返回json格式
@RequestMapping("/blogParam") //公共路径
public class BlogParamController {
    @Autowired
//    注入service层
    private BlogParamService service;

    //    查看全部数据
    @GetMapping("/list")
    public AjaxResult findAll() {
        List<BlogParam> all = service.findAll();
        return AjaxResult.Success(all);
    }

    //    查看分页数据
    @PostMapping("/pagelist")
    public AjaxResult findOne(@RequestBody BlogParamQuery paramQuery) {
        PageInfo<BlogParam> blogParamPageInfo = service.pageList(paramQuery);
        return AjaxResult.Success(blogParamPageInfo);
    }

    //    修改新增
    @PostMapping("/save")
    public AjaxResult saveOrUpdate(@RequestBody BlogParam param) {
       service.saveOrUpdate(param);
       return new AjaxResult();
    }

    //    删除
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            service.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("系统异常");
        }
    }
//    批量删除
@PostMapping("/deleteBatch")
public AjaxResult deleteBatch(@RequestBody BlogParamQuery paramQuery) {
    try {
        Long[] ids = paramQuery.getIds();
        for (Long id : ids) {
            service.delete(id);
        }
        return new AjaxResult();
    } catch (Exception e) {
        e.printStackTrace();
        return AjaxResult.error("系统异常");
    }
}
}

